//
//  HMTodayHappyTalkController.m
//  HowToDo
//
//  Created by qu on 2017/1/4.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMTodayHappyTalkController.h"
#import "HMNetWorkTool.h"
#import "HMTodayHappyTalkModel.h"
#import "HMTabelViewCell.h"
#import "HMHeaderView.h"
#import "HMRecommendDetailVC.h"
#import <SVProgressHUD/SVProgressHUD.h>

@interface HMTodayHappyTalkController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic, weak) HMHeaderView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

static NSString *CellIdentifier = @"CellIdentifier";

@implementation HMTodayHappyTalkController

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
   
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.autoresizesSubviews = NO;
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableView];
    [self setupLayout];
    

    //-------------设置导航栏开始--------------------------------
    self.navigationItem.title = @"今今乐道";
    [super setupNavRightItemsByTwo];
    //-------------设置导航栏结束--------------------------------
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offect = scrollView.contentOffset.y;
    
    if (offect > 64.0f) {
        self.headerView.calendarManager.settings.weekModeEnabled = YES;
        [self.headerView.calendarManager reload];
        self.headerView.calendarContentViewHeight.constant = 65.0f;
        UIView *superView = self.view;
        [self.headerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(superView.mas_top);
            make.left.equalTo(superView.mas_left);
            make.right.equalTo(superView.mas_right);
            // 50+85+22 = 157
            make.height.equalTo(@(137.0f));
        }];
    } else {
        self.headerView.calendarManager.settings.weekModeEnabled = NO;
        [self.headerView.calendarManager reload];
        self.headerView.calendarContentViewHeight.constant = 300.0f;
        UIView *superView = self.view;
        [self.headerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(superView.mas_top);
            make.left.equalTo(superView.mas_left);
            make.right.equalTo(superView.mas_right);
            // 50+300+22 = 372
            make.height.equalTo(@(372.0f));
        }];
    }
    
    [UIView animateWithDuration:0.2f animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
         return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HMTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    HMTodayHappyTalkModel *model =self.dataArr[indexPath.row];
    
    cell.HMModel = model;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HMRecommendDetailVC *recommendDetailVC = [[HMRecommendDetailVC alloc]init];
    
    HMTodayHappyTalkModel *model = self.dataArr[indexPath.row];
    
    recommendDetailVC.resource_id = [NSString stringWithFormat:@"%zd",model.resource_id];
    
    [self.navigationController pushViewController:recommendDetailVC animated:YES];
}
- (HMHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[[NSBundle mainBundle] loadNibNamed:@"HMHeaderView" owner:nil options:nil] firstObject];
    }
    return _headerView;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 208.0f;
        
        [_tableView registerClass:[HMTabelViewCell class] forCellReuseIdentifier:CellIdentifier];
    }
    return _tableView;
}

- (void)setupLayout {
    UIView *superView = self.view;
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView.mas_top);
        make.left.equalTo(superView.mas_left);
        make.right.equalTo(superView.mas_right);
        // 50+300+22 = 372
        make.height.equalTo(@(372.0f));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerView.mas_bottom);
        make.left.equalTo(superView.mas_left);
        make.right.equalTo(superView.mas_right);
        make.bottom.equalTo(superView.mas_bottom);
    }];
}

- (void)loadData {
    
    [HMTodayHappyTalkModel loadHPTalkJsonDataWithfinishedBlock:^(id responseObject) {
        
        self.dataArr = responseObject;
        [self.tableView reloadData];
    }];
}


@end
